package integraltask

import (
	"context"
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/service/backend/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/api/internal/utils/parse"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetIntegralTaskListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetIntegralTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIntegralTaskListLogic {
	return &GetIntegralTaskListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetIntegralTaskListLogic) GetIntegralTaskList(req *types.IntegralTaskListReq) (*types.IntegralTaskListResp, error) {
	logx.Infof("GetIntegralTaskList : %+v ", req)
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	var filters []string
	if req.Status != nil && *req.Status > 0 {
		filters = append(filters, "Status,equalTo:"+strconv.FormatInt(int64(*req.Status), 10))
	}

	preload := "integralTask:Id,RuleName,RuleSlug,RuleType,PicId,PicUrl"
	preload += ",Frequency,Points,EffectiveTime,Sort,Description,Status,CreatedAt,UpdatedAt,DeletedAt"
	eagerLoad := "integralTask:"

	var sorter *string
	if req.Sorter != nil {
		sorter = pointy.GetPointer(strings.TrimSpace(*req.Sorter))
	}
	res, err := l.svcCtx.SettingRpc.GetIntegralTaskList(l.ctx, &settingclient.IntegralTaskListReq{
		Offset:     uint32((page - 1) * pageSize),
		Limit:      uint32(pageSize),
		Preload:    pointy.GetPointer(preload),
		EagerLoad:  pointy.GetPointer(eagerLoad),
		Filter:     pointy.GetPointer(strings.Join(filters, ";")),
		Sorter:     sorter,
		PlatformId: pointy.GetPointer(globalkey.ThirdPartyPlatformWechatMini),
	})
	if err != nil {
		return nil, err
	}

	var hasNextPage bool
	var hasPreviousPage bool
	if res.HasNextPage != nil && *res.HasNextPage > 0 {
		hasNextPage = true
	}
	if res.HasPreviousPage != nil && *res.HasPreviousPage > 0 {
		hasPreviousPage = true
	}
	resp := &types.IntegralTaskListResp{
		BaseListInfo: types.BaseListInfo{
			Total:           res.Total,
			Current:         page,
			PageSize:        pageSize,
			HasNextPage:     hasNextPage,
			HasPreviousPage: hasPreviousPage,
		},
	}

	if len(res.List) > 0 {
		for _, v := range res.List {
			resp.List = append(resp.List, &types.IntegralTaskInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        v.Id,
					CreatedAt: v.CreatedAt,
					UpdatedAt: v.UpdatedAt,
					DeletedAt: v.DeletedAt,
				},
				RuleName:      v.RuleName,
				RuleSlug:      v.RuleSlug,
				RuleType:      v.RuleType,
				PicId:         v.PicId,
				PicUrl:        v.PicUrl,
				Frequency:     v.Frequency,
				Points:        v.Points,
				EffectiveTime: v.EffectiveTime,
				Description:   v.Description,
				Sort:          v.Sort,
				Status:        v.Status,
			})
		}
	}

	return resp, nil
}
